package com.ruoyi.video.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.video.domain.VideoCamera;
import com.ruoyi.video.service.IVideoCameraService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 摄像机Controller
 * 
 * @author ruoyi
 * @date 2024-12-09
 */
@RestController
@RequestMapping("/video/camera")
public class VideoCameraController extends BaseController
{
    @Autowired
    private IVideoCameraService videoCameraService;

    /**
     * 查询摄像机列表
     */
    @GetMapping("/tree")
    public AjaxResult tree(VideoCamera videoCamera)
    {
        return success(videoCameraService.buildingTree(videoCamera));
    }

    /**
     * 查询摄像机列表
     */
    @PreAuthorize("@ss.hasPermi('video:camera:list')")
    @GetMapping("/list")
    public TableDataInfo list(VideoCamera videoCamera)
    {
        startPage();
        List<VideoCamera> list = videoCameraService.selectVideoCameraList(videoCamera);
        return getDataTable(list);
    }

    /**
     * 导出摄像机列表
     */
    @PreAuthorize("@ss.hasPermi('video:camera:export')")
    @Log(title = "摄像机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VideoCamera videoCamera)
    {
        List<VideoCamera> list = videoCameraService.selectVideoCameraList(videoCamera);
        ExcelUtil<VideoCamera> util = new ExcelUtil<VideoCamera>(VideoCamera.class);
        util.exportExcel(response, list, "摄像机数据");
    }

    @Log(title = "摄像机管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:camera:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<VideoCamera> util = new ExcelUtil<VideoCamera>(VideoCamera.class);
        List<VideoCamera> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = videoCameraService.importData(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<VideoCamera> util = new ExcelUtil<VideoCamera>(VideoCamera.class);
        util.importTemplateExcel(response, "摄像机");
    }

    /**
     * 获取摄像机详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:camera:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(videoCameraService.selectVideoCameraById(id));
    }

    /**
     * 新增摄像机
     */
    @PreAuthorize("@ss.hasPermi('video:camera:add')")
    @Log(title = "摄像机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VideoCamera videoCamera)
    {
        videoCamera.setCreateBy(getUsername());
        return toAjax(videoCameraService.insertVideoCamera(videoCamera));
    }

    /**
     * 修改摄像机
     */
    @PreAuthorize("@ss.hasPermi('video:camera:edit')")
    @Log(title = "摄像机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VideoCamera videoCamera)
    {
        videoCamera.setUpdateBy(getUsername());
        return toAjax(videoCameraService.updateVideoCamera(videoCamera));
    }

    /**
     * 删除摄像机
     */
    @PreAuthorize("@ss.hasPermi('video:camera:remove')")
    @Log(title = "摄像机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(videoCameraService.deleteVideoCameraByIds(ids));
    }


    /**
     * 获取摄像机详细信息
     */
    @GetMapping(value = "/play/{id}")
    public AjaxResult playInfo(@PathVariable("id") Long id)
    {
        return success(videoCameraService.getVideoCameraStream(id));
    }
}
